// pages/goods_list/index.js
import { request } from "../../request/index.js"
Page({

    data: {
        tabs: [
            {
                id: 0,
                value: "综合"
            },
            {
                id: 1,
                value: "销量"
            },
            {
                id: 2,
                value: "价格"
            }
        ],
        activeItemIndex: 0,
        goodsList: []
    },

    queryInfo: {
        query: '',
        cid: '',
        pagenum: 1,
        pagesize: 10
    },
    totalPage: 1,
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.queryInfo.cid = options.cid;
        this.getGoodsList()
    },
    // 触底函数
    onReachBottom() {
        if (this.queryInfo.pagenum >= this.totalPage) {
            wx.showToast({
                title: '没有更多数据',
            })
        } else {
            this.queryInfo.pagenum++;
            this.getGoodsList()
        }
    },
    tabItemChange(e) {
        const { index } = e.detail
        this.setData({
            activeItemIndex: index
        })
    },
    async getGoodsList() {
        const res = (await request({ url: '/goods/search', data: this.queryInfo })).data
        let total = res.message.total
        this.totalPage = Math.ceil(total / this.queryInfo.pagesize)
        this.setData({
            goodsList: [...this.data.goodsList, ...res.message.goods],
        })

    }
})